import { GCellRenderer } from "../../../../../com/mui/controls/GCellRenderer";
import { GLabel } from "../../../../../com/mui/controls/GLabel";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { NPCInfo } from "../../../model/NPCInfo";
import { TaskTargetInfo } from "../../../model/TaskTargetInfo";
import { MapFileUtil } from "../../../scene/map/MapFileUtil";
import { EntityType } from "../../../rules/EntityType";
import { AIManager } from "../../../scene/ai/AIManager";
import { Game } from "../../../Game";
import { AIType } from "../../../scene/ai/AIType";
import { ObjectPool } from "../../../../../com/gengine/utils/pools/ObjectPool";
import { FlyFootBmp } from "../flyFoot/FlyFootBmp";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { EMapInstanceType } from "../../../../../Message/Public/EMapInstanceType";
//class NPCRenderer
    
    export  class NPCRenderer extends GCellRenderer
    {
        constructor()
        {
            super();
            this._npcLabel = new GLabel();
            this._npcLabel.filters = [FilterConst.nameGlowFilter];
            this._npcLabel.width = 150;
            this._npcLabel.height = 20;
            this._npcLabel.mouseChildren = false;
            this._npcLabel.mouseEnabled = true;
            this._npcLabel.buttonMode = true;
            addChild(this._npcLabel);
            this.buttonMode = false;
            this.mouseEnabled = false;
            this.mouseChildren = true;
            this._npcLabel.addEventListener(flash.events.MouseEvent.CLICK, this.onMove, false, 0, true);
            return;
        }

        /* internal  */onMove(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=_data as NPCInfo;
            var loc2=new TaskTargetInfo();
            loc2.x = loc1.snpc.point.x;
            loc2.y = loc1.snpc.point.y;
            loc2.id = loc1.tnpc.npcId;
            loc2.mapId = MapFileUtil.mapID;
            loc2.targetType = EntityType.NPC;
            AIManager.onAutoPathAIControl(loc2);
            return;
        }

        /* internal  */onFly(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=_data as NPCInfo;
            var loc2=new TaskTargetInfo();
            loc2.id = loc1.tnpc.npcId;
            loc2.x = loc1.snpc.point.x;
            loc2.y = loc1.snpc.point.y;
            loc2.name = loc1.tnpc.name;
            loc2.mapId = Game.sceneInfo.sMapDefine.mapId;
            loc2.mapName = Game.sceneInfo.sMapDefine.name;
            loc2.targetType = EntityType.NPC;
            AIManager.onAIControl(loc2, AIType.AI_Convey);
            return;
        }

        /* internal  */updateFlyFoot(): void
        {
            if (!this._flyBitmap) 
            {
                this._flyBitmap = ObjectPool.getObject(FlyFootBmp);
            }
            this._flyBitmap.addEventListener(flash.events.MouseEvent.CLICK, this.onFly, false, 0, true);
            this._flyBitmap.x = this._npcLabel.x + this._npcLabel.textField.textWidth;
            addChild(this._flyBitmap);
            return;
        }

        /* internal  */disposeFlyFoot(): void
        {
            if (this._flyBitmap && this._flyBitmap.parent == this) 
            {
                this._flyBitmap.addEventListener(flash.events.MouseEvent.CLICK, this.onFly, false, 0, true);
                ObjectPool.disposeObject(this._flyBitmap);
                removeChild(this._flyBitmap);
                this._flyBitmap.dispose();
            }
            return;
        }

        public /* override */ set data(arg1: Object)
        {
            super.data = arg1;
            var loc1=_data as NPCInfo;
            this._npcLabel.htmlText = "";
            if (loc1 == null || loc1.tnpc == null) 
            {
                this.disposeFlyFoot();
                return;
            }
            if (loc1.tnpc.career == "") 
            {
                this._npcLabel.htmlText = HTMLUtil.addColor(loc1.tnpc.name, "#ffffff");
            }
            else 
            {
                this._npcLabel.htmlText = HTMLUtil.addColor(loc1.tnpc.career, "#ffffff");
            }
            if (Game.sceneInfo.sMapDefine.instanceType.__value == EMapInstanceType._EMapInstanceTypeCopy) 
            {
                this.disposeFlyFoot();
            }
            else 
            {
                this.updateFlyFoot();
            }
            return;
        }

        public /* override */ get height(): number
        {
            return this._flyBitmap.height;
        }

        protected /* var */_npcLabel: GLabel;

        protected /* var */_flyBitmap: FlyFootBmp;
    }
